package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.service.IVerifyCodeService;
import cn.wolfcode.rbac.util.RedisKey;
import cn.wolfcode.rbac.util.VerifyCodeUtil;
import cn.wolfcode.rbac.web.vo.VerifyCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl  implements IVerifyCodeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public VerifyCodeVo getVerifyCodeVo() {
        //1.生成随机码
        String randomCode = VerifyCodeUtil.generateRandomCode();
        //2.生成图片Base64字符串
        String imgStr = VerifyCodeUtil.generateVerifyCode(randomCode);
        //3.生成UUID
        String uuid = UUID.randomUUID().toString().replace("-","");
        //4.拼接Redis的Key
        String key = RedisKey.VERIFYCODE_KEY_PREFIX+uuid;
        //5.将随机码存储到Redis中
        stringRedisTemplate.opsForValue().set(key,randomCode,RedisKey.VERIFYCODE_EXPIRE, TimeUnit.SECONDS);
        //6.把UUID和Base64字符串封装到Vo
        VerifyCodeVo vo = new VerifyCodeVo();
        vo.setUuid(uuid);
        vo.setImg(imgStr);
        return vo;
    }
}
